//
//  SVProgressHUDExtension.swift
//  GomroBoss
//
//  Created by apple on 2020/2/4.
//  Copyright © 2020 cn.gomro.app. All rights reserved.
//

import Foundation
import SVProgressHUD
import YYKit

extension SVProgressHUD {
    
    /// 加载动画
    /// - Parameter withStatus: 展示信息
    static func showLoading(withStatus info:String? = Setting.K_APP_LOADING_INFO) {
        
        if SVProgressHUD.getCurrentStyle() != .custom {
            SVProgressHUD.setDefaultStyle(.custom)
            SVProgressHUD.setDefaultMaskType(.none)
            SVProgressHUD.setBackgroundColor(UIColor.clear)
            SVProgressHUD.setBackgroundLayerColor(UIColor.clear)
            SVProgressHUD.setMinimumDismissTimeInterval(TimeInterval(CGFloat.greatestFiniteMagnitude))
            
            SVProgressHUD.setInfoImage(YYImage.animatedImage(with: [YYImage.init(named: "gomro20000.png")!,
                                                                    YYImage.init(named: "gomro20001.png")!,
                                                                    YYImage.init(named: "gomro20002.png")!,
                                                                    YYImage.init(named: "gomro20003.png")!,
                                                                    YYImage.init(named: "gomro20004.png")!,
                                                                    YYImage.init(named: "gomro20005.png")!,
                                                                    YYImage.init(named: "gomro20006.png")!,
                                                                    YYImage.init(named: "gomro20007.png")!,
                                                                    YYImage.init(named: "gomro20008.png")!,
                                                                    YYImage.init(named: "gomro20009.png")!,
                                                                    YYImage.init(named: "gomro20010.png")!,
                                                                    YYImage.init(named: "gomro20011.png")!,
                                                                    YYImage.init(named: "gomro20012.png")!,
                                                                    YYImage.init(named: "gomro20013.png")!,
                                                                    YYImage.init(named: "gomro20014.png")!,
                                                                    YYImage.init(named: "gomro20015.png")!,
                                                                    YYImage.init(named: "gomro20016.png")!,
                                                                    YYImage.init(named: "gomro20017.png")!,
                                                                    YYImage.init(named: "gomro20018.png")!,
                                                                    YYImage.init(named: "gomro20019.png")!,
                                                                    YYImage.init(named: "gomro20020.png")!,
                                                                    YYImage.init(named: "gomro20021.png")!,
                                                                    YYImage.init(named: "gomro20022.png")!,
                                                                    YYImage.init(named: "gomro20023.png")!,
                                                                    YYImage.init(named: "gomro20024.png")!,
                                                                    YYImage.init(named: "gomro20025.png")!,
                                                                    YYImage.init(named: "gomro20026.png")!,
                                                                    YYImage.init(named: "gomro20027.png")!,
                                                                    YYImage.init(named: "gomro20028.png")!,
                                                                    YYImage.init(named: "gomro20029.png")!,
                                                                    YYImage.init(named: "gomro20030.png")!,
                                                                    YYImage.init(named: "gomro20031.png")!,
                                                                    YYImage.init(named: "gomro20032.png")!], duration: TimeInterval.init(3.3))!)
            
            //修改动画图片尺寸
            SVProgressHUD.setImageViewSize(CGSize.init(width: 113, height: 40))
        }
        
        SVProgressHUD.showInfo(withStatus: info ?? "")
    }
    
    
    /// 结束加载
    static func dismissLoading(isInit:Bool = false){
        
        if SVProgressHUD.getCurrentStyle() != .dark {
            
            SVProgressHUD.setDefaultStyle(.dark)
            SVProgressHUD.setMinimumDismissTimeInterval(1.5)
            
            //还原默认提示图标
            let bundle = Bundle.init(for: SVProgressHUD.classForCoder())
            let url:URL = bundle.url(forResource: "SVProgressHUD", withExtension: "bundle")!
            let imageBundle = Bundle.init(url: url)
            
            let path = imageBundle?.path(forResource: "info", ofType: "png")
            if path != nil && path != "" {
                let infoImg = YYImage.init(contentsOfFile: path!)
                if infoImg != nil {
                    SVProgressHUD.setInfoImage(infoImg!)
                    
                    //还原动画图片尺寸
                    SVProgressHUD.setImageViewSize(CGSize.init(width: 28, height: 28))
                }
            }
            
            SVProgressHUD.dismiss()
        }
    }
    
}
